package com.abbas.abchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abbas.abchat.R;
import com.abbas.abchat.entity.Item;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;

import java.util.List;

/**
 * @author abbas
 * @description: TODO
 * @date 2022/4/17 14:10
 */
public class UserListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Item> items;

    public UserListAdapter(Context mContext, List<Item> items) {
        this.mContext = mContext;
        this.items = items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return 0;
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImgViewHolder imgViewHolder = null;
        CommonViewHolder commonViewHolder = null;

        Item item = items.get(position);
        int type = getItemViewType(position);

        if (convertView == null){
            switch (type){
                case 0:
                    imgViewHolder = new ImgViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.user_infor_img_item,null,false);
                    imgViewHolder.img = convertView.findViewById(R.id.img);
                    imgViewHolder.tag = convertView.findViewById(R.id.tag);
                    convertView.setTag(imgViewHolder);
                    break;
                case 1:
                    commonViewHolder = new CommonViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.user_info_common_item,null,false);
                    commonViewHolder.tag = convertView.findViewById(R.id.tag);
                    commonViewHolder.value = convertView.findViewById(R.id.value);
                    convertView.setTag(commonViewHolder);
                    break;
            }
        }else {
            switch (type){
                case 0:
                    imgViewHolder = (ImgViewHolder) convertView.getTag();
                    break;
                case 1:
                    commonViewHolder = (CommonViewHolder) convertView.getTag();
                    break;
            }
        }

        switch (type){
            case 0:
                Glide.with(mContext)
                        .load(item.getMapId())
                        .transform(new RoundedCorners(18))
                        .into(imgViewHolder.img);
                imgViewHolder.tag.setText(item.getName());
                break;
            case 1:
                commonViewHolder.tag.setText(item.getName());
                commonViewHolder.value.setText(item.getValue());
                break;
        }

        return convertView;
    }



    static class ImgViewHolder{
        private TextView tag;
        private ImageView img;
    }

    static class CommonViewHolder{
        private TextView tag;
        private TextView value;
    }
}
